<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="close"
  >
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :border="true"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="true"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="95"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>

      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        :width="item.width"
      >
        <template #default="{ row }">
          {{ row[item.prop] }}
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
  import { GetConfigInfoHistory } from '@/api/projectConfig/versions'

  export default {
    name: 'ConfigInfoHistoryForm',
    data() {
      return {
        title: '',
        dialogFormVisible: false,
        versionID: '',
        height: this.$baseTableHeight(1),
        lineHeight: 'mini',
        checkList: ['Key', 'Value', '描述'],
        columns: [
          {
            label: 'Key',
            width: '200',
            prop: 'configKey',
          },
          {
            label: 'Value',
            width: '200',
            prop: 'configValue',
          },
          {
            label: '描述',
            width: '200',
            prop: 'configDescription',
          },
        ],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    methods: {
      ShowForm(version) {
        this.title = '历史版本【' + version.versionNo + '】'
        this.versionID = version.id
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      async fetchData() {
        this.listLoading = true
        GetConfigInfoHistory({ VersionID: this.versionID }).then((res) => {
          this.list = res.data
        })
        this.listLoading = false
      },
    },
  }
</script>
